package zero.tech.games.logic.remote;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.games.common.PipeiGroup;
import zero.tech.games.common.RoomNodeUtil;
import zero.tech.games.entity.games.Club;
import zero.tech.games.logic.common.CacheRoom;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.util.LogicUtil;
@RestController
public class RemoteLogic {
	@Value("${server.address}")
	private String host;
	@Value("${server.port}")
	private String port;
	@Value("${spring.application.name}")
	private String service_id;
	@Autowired
	private RoomNodeUtil roomNodeUtil;
	
	@RequestMapping("/check-lastroom-exsist")
	public Boolean checkLastRoomExsist(@RequestBody Map<String,String> map) {
		String room_id=map.get("room_id");
		String club_id=map.get("club_id");
		String uid=map.get("uid");
		Room room=CacheRoom.getCache().getRooms().get(room_id);
		if(room==null) {
			LogicUtil.clearRoom(Integer.valueOf(uid), room_id, club_id);
			return false;
		}
		return true;
	}
	
	@RequestMapping("/check-room-exsist")
	public Boolean checkRoomExsist(@RequestBody Map<String,String> map) {
		String room_id=map.get("room_id");
		String club_id=map.get("club_id");
		Room room=CacheRoom.getCache().getRooms().get(room_id);
		if(room==null) {
			//房间已不存在了,可以清除房间节点
			roomNodeUtil.delete(room_id, club_id);
			return false;
		}
		return true;
	}
	@RequestMapping("/create-nomal-room")
	public Resp createNomalRoom(@RequestBody Map<String,String> map) {
		String simpleUserStr=map.get("simpleUserStr");
		String configStr=map.get("configStr");
		String game_id=map.get("game_id");
		SimpleUser simpleUser=JSONObject.parseObject(simpleUserStr, SimpleUser.class);
		return LogicUtil.createRoom(simpleUser,configStr,host,port,service_id,Integer.valueOf(game_id),null);
	}
	@RequestMapping("/create-club-room")
	public Resp createClubRoom(@RequestBody Map<String,String> map) {
		String simpleUserStr=map.get("simpleUserStr");
		String configStr=map.get("configStr");
		String clubStr=map.get("clubStr");
		String game_id=map.get("game_id");
		SimpleUser simpleUser=JSONObject.parseObject(simpleUserStr, SimpleUser.class);
		Club club=JSONObject.parseObject(clubStr, Club.class);
		return LogicUtil.createRoom(simpleUser,configStr,host,port,service_id,Integer.valueOf(game_id),club);
	}
	@RequestMapping("/join-nomal-room")
	public Resp joinNomalRoom(@RequestBody Map<String,String> map) {
		String simpleUserStr=map.get("simpleUserStr");
		String room_id=map.get("room_id");
		SimpleUser simpleUser=JSONObject.parseObject(simpleUserStr, SimpleUser.class);
		Room room=CacheRoom.getCache().getRooms().get(room_id);
		Assert.notNull(room,"此处房间不为null");
		return LogicUtil.joinRoom(room,simpleUser);
	}
	@RequestMapping("/join-club-room")
	public Resp joinClubRoom(@RequestBody Map<String,String> map) {
		String simpleUserStr=map.get("simpleUserStr");
		String room_id=map.get("room_id");
		SimpleUser simpleUser=JSONObject.parseObject(simpleUserStr, SimpleUser.class);
		Room room=CacheRoom.getCache().getRooms().get(room_id);
		Assert.notNull(room,"此处房间不为null");
		return LogicUtil.joinRoom(room,simpleUser);
	}
	@RequestMapping("/create-pipei-room")
	public Resp createPipeiRoom(@RequestBody Map<String,String> map) {
		String configStr=map.get("configStr");
		String clubStr=map.get("clubStr");
		String game_id=map.get("game_id");
		String group_str=map.get("group_str");
		List<PipeiGroup> pipeiGroups=JSONObject.parseObject(group_str, new TypeReference<List<PipeiGroup>>() {});
		Club club=JSONObject.parseObject(clubStr, Club.class);
		return LogicUtil.createPipeiRoom(pipeiGroups,configStr,host,port,service_id,Integer.valueOf(game_id),club);
	}
	
	@RequestMapping("/quick-join-club-room")
	public Resp quickJoinClubRoom(@RequestBody Map<String,String> map) {
		String simpleUserStr=map.get("simpleUserStr");
		String configStr=map.get("configStr");
		String clubStr=map.get("clubStr");
		String game_id=map.get("game_id");
		String pipei_key=map.get("pipei_key");
		SimpleUser simpleUser=JSONObject.parseObject(simpleUserStr, SimpleUser.class);
		Club club=JSONObject.parseObject(clubStr, Club.class);
		return LogicUtil.quickJoinClubRoom(simpleUser,configStr,host,port,service_id,Integer.valueOf(game_id),club,pipei_key);
	}
}
